package com.qingcloud.adminbackend.controller;

import cn.hutool.core.collection.CollUtil;
import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.entity.aline.TcpValueCommandDTO;
import com.qingcloud.adminbackend.entity.upgrade.UpgradeSpecialDTO;
import com.qingcloud.adminbackend.entity.upgrade.UpgradeSpecialRequest;
import com.qingcloud.adminbackend.service.impl.ITdUpgradeSpecialServiceImpl;
import com.qingcloud.adminbackend.utils.CookieUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(value = "升级站点信息", description = "升级站点信息")
@RestController
@RequestMapping("/upgradeSpecial")
public class TdUpgradeSpecialController {

    @Resource
    private ITdUpgradeSpecialServiceImpl upgradeSpecialService;

    /**
     * 分页查询
     *
     * @param upgradeSpecialRequest
     * @return
     */
    @ApiOperation(value = "分页查询升级站点信息", notes = "分页查询升级站点信息", response = UpgradeSpecialDTO.class)
    @PostMapping(value = "/getUpgradeSpecial")
    public AjaxResult getUpgradeSpecial(@RequestBody UpgradeSpecialRequest upgradeSpecialRequest) {
        return upgradeSpecialService.getUpgradeSpecial(upgradeSpecialRequest);
    }

    /***
     * 添加
     * @param upgradeSpecial
     * @param request
     * @return
     */
    @ApiOperation(value = "新增升级站点信息", notes = "新增升级站点信息")
    @PostMapping(value = "/addUpgradeSpecial")
    public AjaxResult addUpgradeSpecial(@RequestBody UpgradeSpecialDTO upgradeSpecial, HttpServletRequest request) {
        String userName = CookieUtil.getUserName(request);
        upgradeSpecial.setOperator(userName);
        List<TcpValueCommandDTO> dtoList = upgradeSpecialService.addUpgradeSpecial(upgradeSpecial);
        if (CollUtil.isEmpty(dtoList)) {
            return new AjaxResult<>(WamingConstant.FAILURE, "参数中未找到要升级的设备编号", "");
        } else {
            return upgradeSpecialService.sendUpgradeCommand(dtoList);
//            Boolean result = upgradeSpecialService.oldUpgradeCommand(upgradeSpecial.getEquipmentId());
//            if (result) {
//                return new AjaxResult(WamingConstant.SUCCESS, "", "升级指令已发送");
//            } else {
//                return new AjaxResult(WamingConstant.SUCCESS, "升级指令发送失败", "");
//            }
        }
    }


    /**
     * 获取指定升级站点信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取指定升级站点信息", notes = "获取指定id的升级站点信息", response = UpgradeSpecialDTO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "升级站点ID", required = true, dataType = "string", paramType = "path", defaultValue = "1"),
    })
    @GetMapping(value = "/getUpgradeSpecialById/{id}")
    public AjaxResult getUpgradeSpecialById(@PathVariable("id") String id) {
        return upgradeSpecialService.getUpgradeSpecialById(id);
    }

    /**
     * 修改升级站点信息
     *
     * @param upgradeSpecial
     * @return
     */
    @ApiOperation(value = "修改升级站点信息", notes = "修改升级站点信息")
    @PostMapping(value = "/updateUpgradeSpecial")
    public AjaxResult updateUpgradeSpecial(@RequestBody UpgradeSpecialDTO upgradeSpecial, HttpServletRequest request) {
        String userName = CookieUtil.getUserName(request);
        upgradeSpecial.setOperator(userName);
        return upgradeSpecialService.updateUpgradeSpecial(upgradeSpecial);
    }

    /**
     * 删除升级站点信息
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除升级站点信息", notes = "删除升级站点信息")
    @PostMapping(value = "/deleteUpgradeSpecial")
    public AjaxResult deleteUpgradeSpecial(@RequestParam("ids[]") String[] ids) {
        return upgradeSpecialService.deleteUpgradeSpecial(ids);
    }

    @ApiOperation(value = "批量导入升级站点信息", notes = "批量导入升级站点信息")
    @PostMapping(value = "/batchImport")
    public AjaxResult batchImport(@RequestParam("excelFile") MultipartFile excelFile, @RequestParam(value = "binId") String binId, HttpServletRequest request) throws Exception {
        AjaxResult result = new AjaxResult();
        if (excelFile != null && !excelFile.isEmpty()) {
            String userName = CookieUtil.getUserName(request);
            result = upgradeSpecialService.batchImport(excelFile, userName, binId);
        }
        return result;
    }

    @ApiOperation(value = "批量导入升级站点信息", notes = "批量导入升级站点信息")
    @GetMapping(value = "/existStationId/{equipmentId}")
    public AjaxResult existStationId(@PathVariable("equipmentId") String equipmentId) {
        return upgradeSpecialService.existEquipmentId(equipmentId);
    }

}
